package com.yedean.mall.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.yedean.mall.api.CommonPage;
import com.yedean.mall.api.CommonResult;
import com.yedean.mall.form.PmsProductAttributeParam;
import com.yedean.mall.entity.PmsProductAttribute;
import com.yedean.mall.service.IPmsProductAttributeService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 商品属性管理Controller
 *
 * @author yedean
 * @date 2022/8/12
 */

@RestController
@Api(tags = "商品属性管理")
@RequestMapping("/admin/productAttribute")
public class PmsProductAttributeController {

    @Autowired
    IPmsProductAttributeService productAttributeService;

    @ApiOperation("根据分类查询属性列表或参数列表")
    @ApiImplicitParams({@ApiImplicitParam(name = "type", value = "0表示属性，1表示参数", required = true, paramType = "query", dataType = "integer")})
    @RequestMapping(value = "/list/{cid}", method = RequestMethod.GET)
    @ResponseBody
    public CommonResult<CommonPage<PmsProductAttribute>> getList(@PathVariable Long cid,
                                                                 @RequestParam(value = "type") Integer type,
                                                                 @RequestParam(value = "pageSize", defaultValue = "5") Integer pageSize,
                                                                 @RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum) {
        IPage<PmsProductAttribute> attributePage = productAttributeService.listPage(cid, type, pageSize, pageNum);
        return CommonResult.success(CommonPage.resetPage(attributePage));
    }


    @ApiOperation("添加商品属性信息")
    @RequestMapping(value = "/create", method = RequestMethod.POST)
    public CommonResult<?> create(@RequestBody PmsProductAttributeParam productAttributeParam) {
        PmsProductAttribute pmsProductAttribute = new PmsProductAttribute();
        BeanUtils.copyProperties(productAttributeParam, pmsProductAttribute);
        productAttributeService.create(pmsProductAttribute);
        return CommonResult.success();
    }

    @ApiOperation("修改商品属性信息")
    @RequestMapping(value = "/update/{id}", method = RequestMethod.POST)
    @ResponseBody
    public CommonResult<?> update(@PathVariable Long id, @RequestBody PmsProductAttributeParam productAttributeParam) {
        PmsProductAttribute attribute = new PmsProductAttribute();
        BeanUtils.copyProperties(productAttributeParam, attribute);
        attribute.setId(id);
        boolean update = productAttributeService.saveOrUpdate(attribute);
        if (update) {
            return CommonResult.success();
        } else {
            return CommonResult.failed();
        }
    }


    @ApiOperation("查询单个商品属性")
    @RequestMapping(value = "/{id}", method = RequestMethod.GET)
    @ResponseBody
    public CommonResult<PmsProductAttribute> getItem(@PathVariable Long id) {
        PmsProductAttribute productAttribute = productAttributeService.getById(id);
        return CommonResult.success(productAttribute);
    }

    @ApiOperation("批量删除商品属性")
    @RequestMapping(value = "/delete", method = RequestMethod.POST)
    @ResponseBody
    public CommonResult delete(@RequestParam("ids") List<Long> ids) {
        productAttributeService.delete(ids);
        return CommonResult.success();
    }


}
